package org.hackystat.storageservice.resources.alt;

import org.hackystat.storageservice.db.derby.DerbyFacade;
import org.restlet.Context;
import org.restlet.data.Form;
import org.restlet.data.MediaType;
import org.restlet.data.Request;
import org.restlet.data.Response;
import org.restlet.data.Status;
import org.restlet.resource.Representation;
import org.restlet.resource.Resource;
import org.restlet.resource.ResourceException;
import org.restlet.resource.StringRepresentation;
import org.restlet.resource.Variant;

import java.util.Map;

/**
 *
 * Created by IntelliJ IDEA.
 * User: STE2112
 * Date: 05-05-2011
 * Time: 09:17:13
 * To change this template use File | Settings | File Templates.
 */
public class UserResource extends Resource {
    private Util util;
    private DerbyFacade db;

    public UserResource(Context context, Request request, Response response) {
        super(context, request, response);
        this.util = new Util(request, response);
        db = (DerbyFacade) getContext().getAttributes().get("DerbyFacade");
        getVariants().clear(); 
        getVariants().add(new Variant(MediaType.TEXT_XML));
    }

    @Override
    public Representation represent(Variant variant) throws ResourceException {
        System.out.println("alt.UserResource.represent");
        StringRepresentation xmlString;
        try {
            if (variant.getMediaType().equals(MediaType.TEXT_XML)) {
                String uriUser = (String) getRequest().getAttributes().get("user");
                System.out.println("Requested user: " + uriUser);
                xmlString = Util.getStringRepresentation(db.getUser(uriUser));
                return xmlString;
            }
        } catch (Exception e) {
            util.setStatusInternalError(e);
        }
        return null;
    }

    /**
     * Indicate the DELETE method is supported.
     *
     * @return True.
     */
    @Override
    public boolean allowDelete() {
        return true;
    }

    /**
     * Implement the DELETE method that deletes an existing User given their email.
     * Only the authenticated user (or the admin) can delete their User resource.
     */
    @Override
    public void removeRepresentations() {
        System.out.println("alt.UserResource.removeRepresentations");
        try {
            String uriUser = (String) getRequest().getAttributes().get("user");
            db.deleteUser(uriUser);
            getResponse().setStatus(Status.SUCCESS_OK);
        }
        catch (RuntimeException e) {
            util.setStatusInternalError(e);
        }
    }

    /**
     * Indicate the POST method is supported.
     *
     * @return True.
     */
    @Override
    public boolean allowPost() {
        return true;
    }

    /**
     * Implement the POST method that updates the properties associated with a user.
     * <ul>
     * <li> The User must be currently defined in this UserManager.
     * <li> Only the authenticated User or the Admin can update their user's properties.
     * <li> The payload must be an XML representation of a Properties instance.
     * </ul>
     *
     * @param entity The entity to be posted.
     */
    @Override
    public void acceptRepresentation(Representation entity) {
        System.out.println("alt.UserResource.acceptRepresentation");
        try {
            Form form = new Form(entity);
            Map<String, String> fields = form.getValuesMap();
            String email = fields.get("email");
            String password = fields.get("password");
            String role = fields.get("role");
            String xmlUser = fields.get("xmlUser");
            String xmlUserRef = fields.get("xmlUserRef");
            System.out.println("email      = " + email);
            System.out.println("password   = " + password);
            System.out.println("role       = " + role);
            System.out.println("xmlUser    = " + xmlUser);
            System.out.println("xmlUserRef = " + xmlUserRef);
            db.storeUser(email, password, role, xmlUser, xmlUserRef);
        }
        catch (RuntimeException e) {
            util.setStatusInternalError(e);
        }
    }

}
